package com.openxt.nbzj.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.openxt.nbzj.R;
import com.openxt.nbzj.fragment.OpenxtBaseActivity;
import com.openxt.nbzj.model.ConfigModel;
import com.openxt.nbzj.model.RemotingModel;
import com.openxt.nbzj.remoting.RemotingEx;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.alibaba.fastjson.JSONObject;
import qs.openxt.libs.progress.Wait;
import qs.openxt.libs.util.StringUtil;
import qs.openxt.libs.util.Utils;

/**
 * Created by 颖杰_2 on 2016-07-30.
 */
public class NoticeDetailsActivity extends OpenxtBaseActivity implements RemotingEx.OnActionListener {
    @BindView(R.id.TITLE)
    TextView TITLE;
    @BindView(R.id.CONTENT)
    TextView CONTENT;
    @BindView(R.id.CREATETIME)
    TextView CREATETIME;
    @BindView(R.id.SOURCE)
    TextView SOURCE;
    @BindView(R.id.image)
    ImageView image;

    Handler mHandler = new Handler();
    Context mContext = null;
    RemotingModel remotingModel = null;
    String ID = null;
    ImageView right_btn;
    boolean isCollect = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setBaseStyle(R.layout.notice_detail_activity, "公告详情");
        ButterKnife.bind(this);
        setActionUrl("#/noticelist");
        mContext = this;
        remotingModel = new RemotingModel(mContext);

        Bundle bundle = getIntent().getExtras();
        if(bundle == null)
            return;
        ID = bundle.getString("ID");
        setupRight();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                initDetail();
            }
        }, 100);
    }

    private void initDetail(){
        String val = Utils.getLocalStorage(mContext, ConfigModel.NOTICE_GET + "$" + ID);
        if(!StringUtil.isNull(val)){
            initDetailView(val);
            remotingModel.getNoticeGetCollect(this, false, ID);
        }else{
            Wait.sharedInstance().start(mContext, "正在获取详情..., 请稍候");
            remotingModel.getNoticeGet(this, false, ID);
            remotingModel.getNoticeGetCollect(this, false, ID);
        }
    }

    private void initDetailView(String val){
        if(StringUtil.isNull(val))
            return;
        try{
            JSONObject info = JSON.parseObject(val);
            TITLE.setText(info.getString("TITLE"));
            Spanned s = Html.fromHtml(info.getString("CONTENT"));
            CONTENT.setText("\u3000\u3000" + s.toString());
            String time = info.getString("CREATETIME");
            CREATETIME.setText(time.substring(0, 10));
            SOURCE.setText(info.getString("SOURCE"));
        }catch (Exception e){

        }
    }

    private void getNoticeDelCollect(){
        remotingModel.getNoticeDelCollect(this, true, ID);
    }

    private void getNoticeCollectionnotice(){
        remotingModel.getNoticeCollectionnotice(this, true, ID);
    }

    private void setupRight() {
        LinearLayout right = getRight();
        right.setVisibility(View.VISIBLE);
        right.removeAllViews();

        View view = LayoutInflater.from(this).inflate(R.layout.right_btn, null);
        view.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        right.addView(view);

        right_btn = (ImageView)view.findViewById(R.id.right_btn);
        right_btn.setImageResource(R.mipmap.uncare);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isCollect)
                    getNoticeDelCollect();
                else
                    getNoticeCollectionnotice();
            }
        });
    }

    @Override
    public void onActionCompleted(String reVal, String actionName) {
        if(ConfigModel.NOTICE_GETCOLLECT.equals(actionName)){
            if("true".equalsIgnoreCase(reVal)) {
                right_btn.setImageResource(R.mipmap.care);
                isCollect = true;
            }
            return;
        }else if(ConfigModel.NOTICE_DELCOLLECT.equals(actionName)){
            Utils.setLocalStorage(this, "$notice_collect$", "true");
            isCollect = false;
            right_btn.setImageResource(R.mipmap.uncare);
            return;
        }else if(ConfigModel.NOTICE_COLLECTIONNOTICE.equals(actionName)){
            Utils.setLocalStorage(this, "$notice_collect$", "true");
            isCollect = true;
            right_btn.setImageResource(R.mipmap.care);
            return;
        }

        Wait.sharedInstance().stop();
        if((ConfigModel.NOTICE_GET + "$" + ID).equals(actionName)){
            Utils.setLocalStorage(mContext, ConfigModel.NOTICE_GET + "$" + ID, reVal);
            initDetailView(reVal);
        }
    }

    @Override
    public void onActionFailed(String errorVal, String actionName) {
        Wait.sharedInstance().stop();
        showMsg(errorVal);
    }
}
